package com.cims.bpm.security.advice.decrypt;

import com.cims.bpm.config.CimsConfigProperties;
import com.cims.bpm.security.advice.Decrypt;
import com.cims.bpm.security.utils.security.Constants;
import com.cims.bpm.security.utils.sm4.Sm4Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.xerces.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * SM4解密
 */
@Component
@Slf4j
public class Sm4Decrypt implements Decrypt {

    @Autowired
    CimsConfigProperties cimsConfigProperties;

    @Override
    public String execute(String body) {
        String result = null;
        byte[] sm4Key = cimsConfigProperties.getSecurity().getSm4Key().getBytes();
        // byte[] bodyByte = Base64.decode(body);
        try {
            byte[] bodyByte = java.util.Base64.getDecoder().decode(body.getBytes("UTF-8"));
            result = new String(Sm4Utils.sm4EcbDecrypt(sm4Key, bodyByte, Constants.PAD_NO), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("不支持的编码类型", e);
        }
        return result;
    }
}
